package com.business.file.listener;

import com.alibaba.fastjson.JSONObject;
import com.business.file.service.ImagesService;
import com.rabbitmq.client.Channel;
import com.business.common.enums.RabbitMqConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 监听压缩图片
 */
@Service
public class ResizedImageListener {

    @Autowired
    private ImagesService imagesService;

    private Logger logger = LoggerFactory.getLogger(ResizedImageListener.class);


    /**
     * 监听 发送验证码队列
     * @param message
     * @param channel
     */
    @RabbitListener(queues = RabbitMqConstant.RESIZED_IMAGE)
    public void sendResizedImageListener(Message message, Channel channel) throws IOException {
        JSONObject params = JSONObject.parseObject(new String(message.getBody()));
        logger.info(RabbitMqConstant.RESIZED_IMAGE+" 接收参数执行开始,参数： "+params);
        if(null == params){
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            return;
        }
        try{
            String path = params.getString("filePath");
            imagesService.resizedImage(path);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }


}
